<template>
    <div>
        <h2>添加数据</h2>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>机构名称</td>
                    <td><input type="text" v-model="add.name"></td>
                </tr>
                <tr>
                    <td>
                        <input type="file" @change="imga">
                        <img :src="add.img" width="100" height="100" v-if="add.img != ''">
                    </td>
                </tr>
                <tr>
                    <td>特殊服务</td>
                    <td>
                        <select v-model="add.sname">
                            <option :value="info.sname" v-for="info in list">{{ info.sname }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>营业时间</td>
                    <td><input type="date" v-model="add.time"></td>
                </tr>
                <tr>
                    <td>地址</td>
                    <td><input type="text" v-model="add.address"></td>
                </tr>
                <tr>
                    <td>电话</td>
                    <td><input type="text" v-model="add.tel"></td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" :value="true" v-model="add.style">已发布
                        <input type="radio" :value="false" v-model="add.style">未发布
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="ok"></td>
                </tr>
            </tbody>

        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from "vue";
import moment from 'moment';
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();

let list: any = ref([]);
let add: any = reactive(
    {
        "name": "",
        "img": "",
        "sname": "",
        "time": "",
        "address": "",
        "tel": "",
        "style": true,
        "isdel": true
    }
)
onMounted(() => {
    bing();
})
const bing = () => {
    axios({
        url: 'https://localhost:7235/api/Emp/bind',
        method: 'get',
        params: add
    })
        .then(res => {
            console.log(res);
            list.value = res.data;
        })
        .catch(err => {
            console.log(err);

        })
}
const imga = (e: any) => {
    console.log(e);
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj);
    axios({
        url: 'https://localhost:7235/api/Emp/ImgAdd',
        method: 'post',
        data: f
    })
        .then(res => {
            console.log(res);
            add.img = res.data;

        })
        .catch(err => {
            console.log(err);

        })
}
const ok = () => {
    if (add.name == "") {
        alert("机构不能为空");
        return;
    }
    if (add.img == "") {
        alert("图片不能为空");
        return;
    }
    if (add.sanme == "") {
        alert("特殊服务不能为空");
        return;
    }

    if (add.address == "") {
        alert("地址不能为空");
        return;
    }
    if (add.tel == "") {
        alert("电话不能为空");
        return;
    }


    axios({
        url: 'https://localhost:7235/api/Emp/add',
        method: 'post',
        data: add
    })
        .then(res => {
            console.log(res);
            if (res.data > 0) {
                alert("添加成功");
                router.push("/Showview")
            }
        })
        .catch(err => {
            console.log(err);

        })
}
</script>

<style scoped></style>